package com.powernode.controller;


import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.domain.Customer;
import com.powernode.domain.Result;
import com.powernode.domain.User;
import com.powernode.dto.CustomerDto;
import com.powernode.service.CustomerService;
import com.powernode.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Resource
    private UserService userService;

    @Resource
    private CustomerService customerService;

    @GetMapping("queryCustomer.action")
    public Result queryCustomer(CustomerDto customerDto,HttpSession session){
        User User = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        if (User.getRole().equals(Constants.USER_COLE_NORMAL)){
            customerDto.setUserId(User.getId());
        }
        Page<Customer> pageUser = customerService.queryPage(customerDto);
        return new Result(0,"查询成功",pageUser.getTotal(),pageUser.getResult());
    }

    /**
     * 添加用户
     * @param
     * @return
     */
    @PostMapping("addCustomer.action")
    public Result addCustomer(Customer customer, HttpSession session){
        //设置有效状态
        customer.setDeleted(Constants.USER_NUM_1);
        //设置客户对应的业务员
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        customer.setUserId(user.getId());
        //设置创建时间
        customer.setCreateTime(new Date());
        try {
            customerService.insertSelective(customer);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }

    /**
     * 修改用户
     * @param customer
     * @return
     */
    @PostMapping("updateCustomer.action")
    public Result updateCustomer(Customer customer){
        try {
            customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }
    @PostMapping("deleteCustomer.action")
    public Result deleteCustomer(Customer customer){
        customer.setDeleted(Constants.USER_NUM_2);
        customer.setDeleteTime(new Date());
        try {
            customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }
    @PostMapping("changeCustomer.action")
    public Result changeCustomer(Integer userId,Integer[] customerId){
        if (userId==null){
            return new Result(-1,"请选择业务员");
        }else if(customerId==null){
            return new Result(-1,"请选择客户");
        }
        try {
            customerService.changeCustomer(userId,customerId);
            return new Result(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }
}
